package server.web;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import server.domain.AjaxResult;
import server.domain.Page;
import server.domain.Standard;
import server.service.StandardService;

@Controller
@RequestMapping("/standard")
public class StandardController {
	@Autowired
	private StandardService service;
	private AjaxResult response(boolean flag){
		return flag ? AjaxResult.success() : AjaxResult.faile();
	}

	@RequestMapping("/search")
	@ResponseBody
	public Page<Standard> search(
			@RequestParam("query") String query,
			@RequestParam(name = "page", defaultValue = "1") Integer page,
			@RequestParam(name = "size", defaultValue = "10") Integer size) {
		System.out.println(query);


		return service.search(query,page,size);
	}

	@RequestMapping("/add")
	@ResponseBody
	public AjaxResult addStan(Standard s){
//		System.out.println(s);
		boolean flag = service.insert(s);
		return response(flag);
	}

	@RequestMapping("/list")
	@ResponseBody
	public List<Standard> initlized() {
//		System.out.println("standard list"+service.getCount());
		return service.getAll();
	}

	@RequestMapping("/page")
	@ResponseBody
	public Page<Standard> findPage(
			@RequestParam(name = "page", defaultValue = "1") Integer page,
			@RequestParam(name = "size", defaultValue = "10") Integer size) {

//		return service.findPage(page, size);
		return service.search(null, page, size);
	}

	@RequestMapping("/delete")
	@ResponseBody
	public AjaxResult delete(Standard s) {
		boolean flag=service.delete(s);
		return response(flag);
	}

	@RequestMapping("/update")
	@ResponseBody
	public AjaxResult updtate(Standard s) {
		boolean flag=service.update(s);
		return response(flag);
	}

	@RequestMapping("/delBatch")
	@ResponseBody
	public AjaxResult delBatch(@RequestParam("ids")Integer[] ids){
//		System.out.println(Arrays.toString(ids));
		boolean flag=service.delBatch(ids);
		return response(flag);
	}
}
